﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using System.Collections;
using System.Linq;
using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_GoodsCustomFieldValue。
    /// </summary>
    public class GoodsCustomFieldValuesDal
    {


        #region  添加数据

        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(GoodsCustomFieldValue model)
        {
            using (Entities db = new Entities())
            {
                db.GoodsCustomFieldValues.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model"></param>
        public void Edit(GoodsCustomFieldValue model)
        {
            using (Entities db = new Entities())
            {
                bool exists = db.GoodsCustomFieldValues.Where(x => x.GoodsId == model.GoodsId && x.FieldName == model.FieldName).Any();
                if (exists)
                {
                    db.GoodsCustomFieldValues.Attach(model);
                    db.Entry(model).State = EntityState.Modified;
                    db.SaveChangesAsync().Wait();
                }
                else
                {
                    Add(model);
                }
            } 
        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.GoodsCustomFieldValues where Ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.GoodsCustomFieldValues.Remove(x));

                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        public GoodsCustomFieldValue GetInfo(int Id)
        {
            using (Entities db = new Entities())
            {
                return db.GoodsCustomFieldValues.Find(Id);
            }
        }
        #endregion

        /// <summary>
        /// 读取扩展数据
        /// </summary>
        /// <param name="contentDataKey"></param>
        /// <returns></returns>
        public IList<GoodsCustomFieldValue> ExtendFieldData(int contentDataKey)
        {
            using (var db = new Entities())
            {
                return db.GoodsCustomFieldValues.Where(x => x.GoodsId == contentDataKey).ToList();
            }
        }

    }
}